<!DOCTYPE html>
<html lang="en">
<head>
<!--
See https://github.com/AnalyticalGraphicsInc/cesium-google-earth-examples/blob/master/LICENSE.md

Original Work:

Copyright 2008 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->  
  <title>Multiple Globes</title>
  <script src="../../Cesium/Cesium.js"></script>
  <style>
    @import url(../../Cesium/Widgets/widgets.css);
    #toolbar {
        background-color: lightgray;
    }
    #rows, #cols, #width, #height {
        width: 50px;
    }
    .cesiumContainer {
        width: 400px; height: 300px; margin: 0;
    }
  </style>
</head>
<body>
<table id="toolbar">
    <tr>
        <td> Rows: <input id="rows" type="text" value="2">
        Cols: <input id="cols" type="text" value="2"/>
        Width: <input id="width" type="text" value="400"/>
        Height: <input id="height" type="text" value="300"/>
        <input type="button" value="Refresh" onclick="refresh()"/>
        </td>
    </tr>
    <tr>
        <td> Go to: <input id="loc" type="text"/>
        <input type="button" value="Go" onclick="geocode()"/></td>
    </tr>
    <tr>
        <td>Random Zipcode Tour:
        <input id="tour" type="checkbox" onclick="tour()"/>
        </td>
    </tr>
</table>
<table id="cesiumTable"></table>
  <script>
  	Cesium.BingMapsApi.defaultKey = 'AgH8JB-gA1Wo9ZbGL9oCEqfD-4YAkMnMp_XzzoGbynt_CC6l27GFWqN8SJaYm6aC';
    var widgets = [];
    function refresh() {
        var rows = document.getElementById('rows').value;
        var cols = document.getElementById('cols').value;
        var width = document.getElementById('width').value;
        var height = document.getElementById('height').value;
        var table = document.getElementById('cesiumTable');
        table.innerHTML = '';
        for (var i = 0; i < rows; i++) {
            var tr = document.createElement('tr');
            cesiumTable.appendChild(tr);
            for (var j = 0; j < cols; j++) {
                var td = document.createElement('td');
                tr.appendChild(td);
                var div = document.createElement('div');
                div.style.height = height + 'px';
                div.style.width = width + 'px';
                td.appendChild(div);
                widgets.push(new Cesium.CesiumWidget(div));
            }
        }
        geocode();
    }
    refresh();
    
    function tour(){
        if(document.getElementById('tour').checked){
            var zip = Math.floor(Math.random() * 90000 + 10000);
            document.getElementById('loc').value = zip;
            geocode();
            setTimeout(function() { tour(); }, 5000);
        }
    }
    function geocode() { // Adapted from Cesium.GeocoderViewModel
        var query = document.getElementById('loc').value;
        if (/^\s*$/.test(query)) {
            //whitespace string
            return;
        }

        var promise = Cesium.loadJsonp('//dev.virtualearth.net/REST/v1/Locations', {
            parameters : {
                query : query,
                key : Cesium.BingMapsApi.getKey()

            },
            callbackParameterName : 'jsonp'
        });

        Cesium.when(promise, function(result) {
            if (result.resourceSets.length === 0) {
                document.getElementById('loc').value += ' (not found)';
                return;
            }

            var resourceSet = result.resourceSets[0];
            if (resourceSet.resources.length === 0) {
                document.getElementById('loc').value += ' (not found)';
                return;
            }

            var resource = resourceSet.resources[0];
            document.getElementById('loc').value = resource.name;
            var bbox = resource.bbox;
            var rectangle = Cesium.Rectangle.fromDegrees(bbox[1], bbox[0], bbox[3], bbox[2]);

            for (var i = 0; i < widgets.length; i++){
                widgets[i].camera.flyTo({
                    destination : rectangle,
                    endTransform : Cesium.Matrix4.IDENTITY,
                    convert : false
                });
            }
        }, function() {
            document.getElementById('loc').value += ' (error)';
        });
    }
    
    // Comparable Google Earth API code:
    //
    // https://code.google.com/p/earth-api-samples/source/browse/trunk/demos/multiple/index.html

  </script>
</body>
</html>